<?php

namespace App\Http\Controllers\User;

use App\Model\User;
use Firebase\JWT\JWT;

class TokenController
{

    public function token($request, $response, $args)
    {
        $params = $request->getParams();

        $user = User::where('username', $params['username'])->first();

        $iat = time();
        $exp = time() + 120;

        $header = [
            'typ' => 'JWT',
            'alg' => 'HS256'
        ];

        $payload = [
            'iat' => $iat,
            'exp' =>  $exp,
            'sub' => ['id' => $user->id],
            'jti' => base64_encode(random_bytes(32)),
        ];

        $token =[
            'status' => '200',
            'token' => JWT::encode($payload, getenv('JWT_USER_SECRET'))
        ];

        return json_encode($token);
    }

}
